//#include <string>
//#include <regex>
//#include <iostream>
//using namespace std;
//
//
//void main(){
//
//	string name = string("D:\\Desktop\test2\\VC_Stacc_A#3-A5-011.wav");
//	string tmp = "";
//	std::smatch m;
//	std::regex e(".*");
//	//std::regex e("\\b(sub)([^] * )");
//	// write output file to disk
//	while (std::regex_search(name, m, e)) {
//		for (auto x : m)
//			//tmp += x;
//			std::cout << x << " ";
//	}
//	printf("%s", tmp.c_str());
//int a = 0;
//
//
//}




//#include <iostream>
//#include <string>
////#include <regex>
////
////int main()
////{
////	std::string s("D:\\Desktop\\test2\\VC_Stacc_A#3-A5-011.wav");
////	std::smatch m;
////	std::regex e(".*\\\\"); 
////
////
////	std::cout << "The following matches and submatches were found:" << std::endl;
////
////	while (std::regex_search(s, m, e)) {
////		for (auto x : m) std::cout << x << " ";
////		std::cout << std::endl;
////		s = m.suffix().str();
////	}
////
////	return 0;
////}
//
//// regex_replace example
////#include <iostream>
////#include <string>
////#include <regex>
////#include <iterator>
////#include <string>
////int main()
////{
////	std::string s("D:\\Desktop\\test2\\VC_Stacc_A#3-A5-011.wav");
////	std::regex e("(.*\\\\)(.*)(\.wav)");   // matches words beginning by "sub"
////
////	// with flags:
////	std::string tmp1 = std::regex_replace(s, e, "$1", std::regex_constants::format_no_copy);
////	std::string tmp2 = std::regex_replace(s, e, "$2", std::regex_constants::format_no_copy);
////	std::string tmp3 = std::regex_replace(s, e, "$3", std::regex_constants::format_no_copy);
////	std::cout << std::endl;
////
////	return 0;
////}
//
//
//#include <stdlib.h>
//#include <stdio.h>
//float  *freq_array = NULL;
//int num_freq = 0;
//int  freq_array_rounded[] = {1,4,3,67,8,4,6,8,5,4,6,6,8,5,3,4,5};
//int num_freq_rounded = 17;
//
//
//int compare(const void * a, const void *b){
//	int c = *(int *)a;
//	int d = *(int *)b;
//	/*if (c < d) return 1;
//	if (c = d) return 0;/// this is mistake
//	if (c > d) return -1;*/
//	return c - d;// i forget thiis
//}
//void sort_freq_array_rounded(){
//
//
//	qsort(freq_array_rounded, num_freq_rounded, sizeof(freq_array_rounded[0]), compare);
//
//
//}
//
//void appear_the_most(){
//	int hold = 0;
//	int i = 0;
//	int max = 0;
//	while (i < num_freq_rounded - 1){
//		int count = 0;
//		while (freq_array_rounded[i] == freq_array_rounded[i + 1]){
//			i++;
//			count++;
//		}
//		if (max < count){
//			max = count;
//			hold = freq_array_rounded[i - 1];
//		}
//		i++;
//	}
//
//}
//
//void main(){ 
//	sort_freq_array_rounded();
//	appear_the_most();
//}




#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <regex>
#define MaxNameFile 2000
using namespace std;

#define SIZE_ARRAY_FREQUENCY_MIDINOTE 128
double freqchart[] = {
	8.1757989156, 8.661957218, 9.1770239974, 10.3008611535, 10.3008611535, 10.9133822323, 11.5623257097, 12.2498573744, 12.9782717994, 13.75, 14.5676175474, 15.4338531643,
	16.3515978313, 17.3239144361, 18.3540479948, 19.4454364826, 20.6017223071, 21.8267644646, 23.1246514195, 24.4997147489, 25.9565435987, 27.5, 29.1352350949, 30.8677063285,
	32.7031956626, 34.6478288721, 36.7080959897, 38.8908729653, 41.2034446141, 43.6535289291, 46.249302839, 48.9994294977, 51.9130871975, 55, 58.2704701898, 61.735412657,
	65.4063913251, 69.2956577442, 73.4161919794, 77.7817459305, 82.4068892282, 87.3070578583, 92.4986056779, 97.9988589954, 103.826174395, 110, 116.5409403795, 123.470825314,
	130.8127826503, 138.5913154884, 146.8323839587, 155.563491861, 164.8137784564, 174.6141157165, 184.9972113558, 195.9977179909, 207.65234879, 220, 233.081880759, 246.9416506281,
	261.6255653006, 277.1826309769, 293.6647679174, 311.1269837221, 329.6275569129, 349.228231433, 369.9944227116, 391.9954359817, 415.3046975799, 440, 466.1637615181, 493.8833012561,
	523.2511306012, 554.3652619537, 587.3295358348, 622.2539674442, 659.2551138257, 698.456462866, 739.9888454233, 783.9908719635, 830.6093951599, 880, 932.3275230362, 987.7666025122,
	1046.5022612024, 1108.7305239075, 1174.6590716696, 1244.5079348883, 1318.5102276515, 1396.912925732, 1479.9776908465, 1567.981743927, 1661.2187903198, 1760, 1864.6550460724, 1975.5332050245,
	2093.0045224048, 2217.461047815, 2349.3181433393, 2489.0158697766, 2637.020455303, 2793.825851464, 2959.9553816931, 3135.963487854, 3322.4375806396, 3520, 3729.3100921447, 3951.066410049,
	4186.0090448096, 4434.92209563, 4698.6362866785, 4978.0317395533, 5274.0409106059, 5587.6517029281, 5919.9107633862, 5919.9107633862, 6644.8751612791, 7040, 7458.6201842894, 7902.132820098,
	8372.0180896192, 8869.8441912599, 9397.272573357, 9956.0634791066, 10548.0818212118, 11175.3034058561, 11839.8215267723, 12543.853951416
};
char * midichart[] = {
	"C-2", "C#-2", "D-2", "D#-2", "E-2", "F-2", "F#-2", "G-2", "G#-2", "A-2", "A#-2", "B-2",
	"C-1", "C#-1", "D-1", "D#-1", "E-1", "F-1", "F#-1", "G-1", "G#-1", "A-1", "A#-1", "B-1",
	"C0", "C#0", "D0", "D#0", "E0", "F0", "F#0", "G0", "G#0", "A0", "A#0", "B0",
	"C1", "C#1", "D1", "D#1", "E1", "F1", "F#1", "G1", "G#1", "A1", "A#1", "B1",
	"C2", "C#2", "D2", "D#2", "E2", "F2", "F#2", "G2", "G#2", "A2", "A#2", "B2",
	"C3", "C#3", "D3", "D#3", "E3", "F3", "F#3", "G3", "G#3", "A3", "A#3", "B3",
	"C4", "C#4", "D4", "D#4", "E4", "F4", "F#4", "G4", "G#4", "A4", "A#4", "B4",
	"C5", "C#5", "D5", "D#5", "E5", "F5", "F#5", "G5", "G#5", "A5", "A#5", "B5",
	"C6", "C#6", "D6", "D#6", "E6", "F6", "F#6", "G6", "G#6", "A6", "A#6", "B6",
	"C7", "C#7", "D7", "D#7", "E7", "F7", "F#7", "G7", "G#7", "A7", "A#7", "B7",
	"C8", "C#8", "D8", "D#8", "E8", "F8", "F#8", "G8"
};




char** loadfile(char *path,int &lines){
	FILE* file = NULL;

	char tmpName[MaxNameFile] = { 0 };
	file = fopen(path,"r");

	char ** listname = (char**)malloc(sizeof(char*)*MaxNameFile/*Not good but i will improve later*/);
	memset(listname, 0, MaxNameFile * sizeof(char*)); // init to null list pointer
	lines = 0;
	if (file != NULL){
		while (fgets(tmpName,MaxNameFile,file) != NULL){
			listname[lines] = (char *)malloc(strlen(tmpName)+1);// Malloc memmory
			strcpy(listname[lines], tmpName); // copy to list
			lines++;
		}
	}
	else{
	
		printf("Open file: %s error, file does not exist \n", path);
		return NULL;

	}
	if (file != NULL)
		fclose(file);
	return listname;

}

double mtof(char *midinote){//midi to frequency
	int i = 0;
	for (; i < SIZE_ARRAY_FREQUENCY_MIDINOTE; i++){
		if (strcmp(midichart[i], midinote) == 0){
			break;
		}
	}

	if (i >= 0 && i < SIZE_ARRAY_FREQUENCY_MIDINOTE){
		printf("%d \n", i);
		return freqchart[i];
	}

	return -1;
}


//char* nameofpath(char * path){
//	int i = strlen(path) - 1;
//	while (i >= 0 && path[i] != '\\'){
//		i--;
//	}
//
//	char * tmp = (char*)malloc(strlen(path) - i);
//	memcpy(tmp, path + i+1, strlen(path) - i);
//	tmp[strlen(path) - i] = '\n';
//	return tmp;
//}

void writelof(char* path,char* name){
	int lines = 0;
	char ** listname = loadfile(path, lines);
	std::regex matchFile("(.+\\\\)?(.+)(\\.[\\w\\d]+).*");
	std::regex matchMidi(".*n=([A-Za-z]#?\\-?\\d).*");
	std::regex matchCh(".*ch=(\\d+).*");
	std::regex matchFS(".*fs=(\\d+).*");


	FILE *file = NULL;
	file = fopen(name, "w");
	if (file != NULL){
		for (int i = 0; i < lines; i++){
			char tmp[2000] = { 0 };
			///std::string pathname = std::regex_replace(string(listname[i]), matchFile, "$1");
			std::string filename = std::regex_replace(string(listname[i]), matchFile, "$2");
			//std::string ext = std::regex_replace(string(listname[i]), matchFile, "$3");
			std::string midinoteArg = std::regex_replace(string(listname[i]), matchMidi, "$1");
			//std::string channelArg = std::regex_replace(string(listname[i]), matchCh, "$1");
			//std::string framesizeArg = std::regex_replace(string(listname[i]), matchFS, "$1");
			sprintf(tmp, "%s %3.10f", filename.c_str(), mtof(const_cast<char*>(midinoteArg.c_str())));
			//sprintf(tmp, " \"%s\" n = %2.10f ch=0 fs=2048 ", listname[i],mtof())
			//fputs()
			fputs(tmp, file);

		}
	}
	if (file != NULL)
		fclose(file);
}

void main(){
	//char ** listname = NULL;
	//int lines = 0;
	//listname = loadfile("C:\\filelist.txt", lines);

	//for (int i = 0; i < lines; i++){
	//
	//	printf("%s \n", listname[i]);
	//}


	//printf("%s \n", nameofpath("Z:\\Jeffery Hayat\\SF_Tuba Solo-Legato\\consolidated\\cons-BML203tuba_longs_p_RR1_a_G3.wav"));
	writelof("C:\\filelist.txt", "fff.txt");

}

//char* nameofpath(char * path){
//	int i = strlen(path) - 1;
//	while (i >= 0 && path[i] != '\\'){
//		i--;
//	}
//
//	char * tmp = (char*)malloc(strlen(path) - i);
//	memcpy(tmp, path + i+1, strlen(path) - i);
//	tmp[strlen(path) - i] = '\n';
//	return tmp;
//}

